package com.es.filter; /**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-22 11:22
 */

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter",urlPatterns = {"/AServlet","/BServlet","/CServlet"})
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        System.out.println("我访问的资源是："+uri);
        //进行 登录验证处理？
        HttpSession session = req.getSession();
        String currentUser = (String) session.getAttribute("currentUser");
        HttpServletResponse resp = (HttpServletResponse) response;
        if(currentUser == null){
            resp.sendRedirect("fail.html");
        }
        chain.doFilter(request, response);
    }
}
